/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveWebServicesV12Helpers
{
	public class MessageHeader
	{
		private GrooveWebServicesV12Helpers.Account m_Account;
		private GrooveWebServicesV12Helpers.MessageContact m_Sender;
		private GrooveWebServicesV12Helpers.MessageContact[] m_Recipients;

		private GrooveMessagesWebService.MessageHeader m_MessageHeader;

		public enum MessageCategory
		{
			Unknown = 0x0,
			New = 0x1,
			Opened = 0x2,
			Sent = 0x3,
			Inbox = 0x4,
			All = 0xFF
		}

		public MessageHeader(GrooveWebServicesV12Helpers.Account i_Account, GrooveMessagesWebService.MessageHeader i_MessageHeader)
		{
			m_Account = i_Account;
			m_MessageHeader = i_MessageHeader;
		}

		public MessageHeader(GrooveWebServicesV12Helpers.Account i_Account, string i_RecipientURI)
			: this(i_Account, new string[] {i_RecipientURI}, false, false)
		{
		}

		public MessageHeader(GrooveWebServicesV12Helpers.Account i_Account, string[] i_RecipientURIs)
			: this(i_Account, i_RecipientURIs, false, false)
		{
		}


		public MessageHeader(GrooveWebServicesV12Helpers.Account i_Account, string i_RecipientURI, bool i_HasVoiceMemo, bool i_HasAttachments)
			: this(i_Account, new string[] {i_RecipientURI}, i_HasVoiceMemo, i_HasAttachments)
		{
		}

		public MessageHeader(GrooveWebServicesV12Helpers.Account i_Account, string[] i_RecipientURIs, bool i_HasVoiceMemo, bool i_HasAttachments)
		{
			m_Account = i_Account;
			m_MessageHeader = new GrooveMessagesWebService.MessageHeader();
			
			System.Collections.ArrayList MessageContactArrayList = new System.Collections.ArrayList();
			foreach (string RecipientURI in i_RecipientURIs)
			{
				GrooveMessagesWebService.MessageContact NewMessageContact = new GrooveWebServicesV12Helpers.GrooveMessagesWebService.MessageContact();
				NewMessageContact.URI = RecipientURI;

				MessageContactArrayList.Add(NewMessageContact);
			}

			//  Populate message header
			m_MessageHeader.Recipients = (GrooveMessagesWebService.MessageContact[])MessageContactArrayList.ToArray(typeof(GrooveMessagesWebService.MessageContact));
			m_MessageHeader.HasVoiceMemo = i_HasVoiceMemo;
			m_MessageHeader.HasAttachments = i_HasAttachments;
		}

		#region MessageHeader Properties And Methods

		public GrooveWebServicesV12Helpers.Account Account
		{
			get
			{
				return m_Account;
			}
		}

		internal GrooveMessagesWebService.MessageHeader RawMessageHeader
		{
			get
			{
				return m_MessageHeader;
			}
		}

		public GrooveWebServicesV12Helpers.MessageHeader.MessageCategory Category
		{
			get
			{
				switch (m_MessageHeader.Category)
				{
					case GrooveMessagesWebService.MessageCategory.All:
					{
						return GrooveWebServicesV12Helpers.MessageHeader.MessageCategory.All;
					}

					case GrooveMessagesWebService.MessageCategory.Inbox:
					{
						return GrooveWebServicesV12Helpers.MessageHeader.MessageCategory.Inbox;
					}

					case GrooveMessagesWebService.MessageCategory.New:
					{
						return GrooveWebServicesV12Helpers.MessageHeader.MessageCategory.New;
					}

					case GrooveMessagesWebService.MessageCategory.Opened:
					{
						return GrooveWebServicesV12Helpers.MessageHeader.MessageCategory.Opened;
					}

					case GrooveMessagesWebService.MessageCategory.Sent:
					{
						return GrooveWebServicesV12Helpers.MessageHeader.MessageCategory.Sent;
					}

					default:
					{
						return GrooveWebServicesV12Helpers.MessageHeader.MessageCategory.Unknown;
					}
				}
			}
		}

		public bool HasAttachments
		{
			get
			{
				return m_MessageHeader.HasAttachments;
			}
		}

		public bool HasVoiceMemo
		{
			get
			{
				return m_MessageHeader.HasVoiceMemo;
			}
		}

		public string ID
		{
			get
			{
				return m_MessageHeader.ID;
			}
		}

		public GrooveWebServicesV12Helpers.MessageContact[] Recipients
		{
			get
			{
				if (m_Recipients == null)
				{
					//  Create array list of Contacts
					System.Collections.ArrayList MessageContactArrayList = new System.Collections.ArrayList();
					foreach (GrooveMessagesWebService.MessageContact MessageContact in m_MessageHeader.Recipients)
					{
						GrooveWebServicesV12Helpers.MessageContact NewMessageContact = new GrooveWebServicesV12Helpers.MessageContact
							(
							m_Account,
							MessageContact
							);

						MessageContactArrayList.Add(NewMessageContact);
					}

					//  Convert array list to array
					m_Recipients = (GrooveWebServicesV12Helpers.MessageContact[])MessageContactArrayList.ToArray(typeof(GrooveWebServicesV12Helpers.MessageContact));

				}

				return m_Recipients;
			}
		}

		public GrooveWebServicesV12Helpers.MessageContact Sender
		{
			get
			{
				if (m_Sender == null)
				{
					m_Sender = new GrooveWebServicesV12Helpers.MessageContact
						(
						m_Account,
						m_MessageHeader.Sender
						);
				}

				return m_Sender;
			}
		}

		public string URI
		{
			get
			{
				return m_MessageHeader.URI;
			}
		}

		#endregion

	}
}
